package com.dp.service.impl3;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.pojo.NewsVo;
import com.dp.util.DataMap;
import com.dp.vo.NewsResources;
import com.dp.vo.TMagazinepackage;

@Service
@Transactional
public class MagazinepackageServiceImpl extends BaseImpl {
	
	//获得所有杂志包信息总记录数
	public int findAllMagazinepackageCount(TMagazinepackage magazinepackage) {	
		sql="select count(id) from t_magazinepackage where 1=1 ";
		if(magazinepackage!=null){
			if(magazinepackage.getMname()!=null&&magazinepackage.getMname().toString().trim().length() > 0){
				sql+=" and mname like '%"+magazinepackage.getMname().toString().trim()+"%'";
			}
			if(magazinepackage.getMtype()!=null&&magazinepackage.getMtype().toString().trim().length() > 0){
				sql+=" and mtype like '%"+magazinepackage.getMtype().toString().trim()+"%'";
			}
		}
		return getOneInt();
	}
	
	//获得所有杂志包信息
	public List<TMagazinepackage> findAllMagazinepackageList(DataMap dataMap,
			Integer firstrow, Integer maxrow) {
		TMagazinepackage magazinepackage=(TMagazinepackage) dataMap.get("magazinepackage");
		JsonPage jpage=(JsonPage) dataMap.get("jpage");
		sql = "SELECT t FROM TMagazinepackage t where 1=1 ";
		if(magazinepackage!=null){
			if(magazinepackage.getMname()!=null&&magazinepackage.getMname().toString().trim().length() > 0){
				sql+=" and mname like '%"+magazinepackage.getMname().toString().trim()+"%'";
			}
			if(magazinepackage.getMtype()!=null&&magazinepackage.getMtype().toString().trim().length() > 0){
				sql+=" and mtype like '%"+magazinepackage.getMtype().toString().trim()+"%'";
			}
		}
		if(jpage!=null){
			if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
				sql+=" order by "+jpage.getSidx()+" "+jpage.getSord()+"";
			}
		}
		Query query = em.createQuery(sql);
		if(firstrow!=null&&maxrow!=null){
			query.setFirstResult(firstrow);
			query.setMaxResults(maxrow);
		}
		return query.getResultList();
	}
	
	/**
	 * 获得所有Webview内容信息总记录数
	 * @param hashMap
	 * @return Integer
	 */
	public Integer findAllNewsEntityCount(DataMap hashMap){
		NewsVo news=(NewsVo) hashMap.get("news");
		Object s_role_id = hashMap.get("role_id");
		Object s_user_id = hashMap.get("user_id");
		String userids = (String)hashMap.get("userids");
		Integer iCount = 0; 
		try {
			
			StringBuffer sqlS =new StringBuffer("select count(nc.cid) from t_contents tc,news_contenttitle nc ");
			sqlS.append(" where nc.contentid=tc.contentid and nc.newstype='iphonewebview2' ");
			//审核通过和测试通过的内容
			sqlS.append(" and tc.content_status='pass' and tc.test_status='1' ");
			if(s_role_id!=null&&!"".equals(s_role_id)) {
				sqlS.append(" and tc.user_id = '"+ s_user_id +"'");
			}else if(userids!=null){
				sqlS.append(" and tc.user_id in ("+ userids +")");
			}
			if(news!=null){
				if(news.getContentName()!=null&& news.getContentName().toString().trim().length() > 0){
					sqlS.append(" AND nc.contenttitle like '%"+news.getContentName().toString().trim() + "%'");
				}
				if(news.getCid()!=null&& news.getCid().toString().trim().length() > 0){
					sqlS.append(" AND nc.cid = "+news.getCid().toString().trim() + "");
				}
			}
			logger.info("findAllNewsEntityCount excute sql: " + sqlS.toString());
			
				iCount=this.getOneInt();
		} catch (Exception e) {
			logger.error("findAllNewsEntityCount fail !!!" + e.getMessage());
			e.printStackTrace();
		}
		return iCount;
	}
	
	/**
	 * 获得所有Webview内容信息
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsVo>
	 */
	public List<NewsVo> findAllNewsEntity(DataMap hashMap, Integer firstrow,Integer maxrow){
		JsonPage jpage=(JsonPage) hashMap.get("jpage");
		NewsVo news=(NewsVo) hashMap.get("news");
		List<NewsVo> newsList = null; 
		Object s_role_id = hashMap.get("role_id");
		Object s_user_id = hashMap.get("user_id");
		String userids = (String)hashMap.get("userids");
		try {
			
			StringBuffer sqlS =new StringBuffer("select nc.contentid,nc.cid,nc.contenttitle,nc.updatetime from t_contents tc,news_contenttitle nc ");
			sqlS.append(" where nc.contentid=tc.contentid and nc.newstype='iphonewebview2' ");
			sqlS.append(" and tc.content_status='pass' and tc.test_status='1' ");
			if(s_role_id!=null&&!"".equals(s_role_id)) {
				sqlS.append(" and tc.user_id = '"+ s_user_id +"'");
			}else if(userids!=null){
				sqlS.append(" and tc.user_id in ("+ userids +")");
			}
			
			if(news!=null){
				if(news.getContentName()!=null&& news.getContentName().toString().trim().length() > 0){
					sqlS.append(" AND nc.contenttitle like '%" + news.getContentName().toString().trim() + "%'");
				}
				if(news.getCid()!=null&& news.getCid().toString().trim().length() > 0){
					sqlS.append(" AND nc.cid = "+news.getCid().toString().trim() + "");
				}
			}
			
			if(jpage!=null){
				if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
					sqlS.append(" order by "+jpage.getSidx()+" "+jpage.getSord()+"");
				}
			}
			if(firstrow!=null&&maxrow!=null){
				sqlS.append(" limit "+firstrow+","+maxrow+"");
			}
			logger.info("findAllNewsEntity excute dql: " + sqlS.toString());
			ArrayList conList = this.queryALLResultSet(sqlS.toString());
			newsList=new ArrayList<NewsVo>();
			NewsVo newsVo=null;
			SimpleDateFormat fmt_temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(conList.size()>0)
			{
				for(int i=0;i<conList.size();i++)
				{
					HashMap tmp = (HashMap)conList.get(i);
					newsVo=new NewsVo();
					newsVo.setContentId((String)tmp.get("contentid"));
					newsVo.setCid(Integer.parseInt((String)tmp.get("cid")));
					newsVo.setContentName((String)tmp.get("contenttitle"));
					newsVo.setLastupdatetime(fmt_temp.parse((String)tmp.get("updatetime")));
					newsList.add(newsVo);
				}
			}
		
		} catch (Exception e) {
			logger.error("findAllNewsEntity fail !!!" + e.getMessage());
			e.printStackTrace();
		}
		return newsList;
	}

	//增加杂志包记录
	public boolean insertMagazinepackage(TMagazinepackage magazinepackage) {
		em.persist(magazinepackage);
		return true;
	}
	
	public String getWeburlByCid(String cid)
	{
		this.sql = "SELECT weburl FROM news_contenttitle where cid=:cid";
		Object o=null;
		try {
			Query query = em.createNativeQuery(sql);
			query.setParameter("cid", cid);
			o = query.getSingleResult();
		} catch (Exception e) {
			log.error(" getWeburlByCid fail !!!" +e.getMessage());
			e.printStackTrace();
			return null;
		}
		return (String)o;
	}

	public List<NewsResources> findResourceByNews(Integer cid)
	{
		List<NewsResources> ll=new ArrayList<NewsResources>();
		sql = "SELECT nr FROM NewsResources nr,NewsContentResource ncr where ncr.cid="+cid+" and ncr.rid=nr.rid ";
		try {
			ll=em.createQuery(sql).getResultList();
		} catch (Exception e) {
			log.error(" findResourceByNews fail !!!" +e.getMessage());
			e.printStackTrace();
		}
		return ll;
	}

	public boolean deleteMagazinepackage(Integer id) {
		sql="delete from t_magazinepackage where id="+id+"";
		em.createNativeQuery(sql).executeUpdate();
		return true;
	}
}